/**
 * 作者: hxl
 * 日期: 2024-12-11 18:10:28
 * 备注: 用户花店任务表
 */
package com.motu.monstercity.server.game.userdata;

import com.motu.monstercity.protocol.Common.PBUserFloristTask;
import com.motu.vertx.module.utility.model.BaseModel;
import com.motu.vertx.module.utility.model.Model;
import io.vertx.core.json.JsonObject;
import io.vertx.core.json.JsonArray;

public class UserFloristTaskModel extends BaseModel implements Model {
    public static final String LISTKEY = "user_id.task_id";
    public static final int REDIS_EXPIRE_TIME = 0;// redis过期时间
    public static final int UPDATE_DB_TYPE = UPDATE_DB_TYPE_QUEUE;// 数据入库类型 队列入库
    public static final boolean ORDER_LIST = true;// 是否需要额外的有序列表

    protected long id; // id
    protected long userId; // 玩家id
    protected int taskId; // 任务表的id
    protected long curP; // 当前进度
    protected byte isRecv; // 是否领取奖励

    public void reset() {
    }

    protected String getKey() {
        return "UserFloristTask." + userId + "." + taskId;
    }

    protected String getListKey() {
        return "UserFloristTask." + userId;
    }

    protected String getListItemKey() {
        return taskId + "";
    }

    public void doCreate() {
        setTableId();
        insert();
    }

    public void setTableId() {
        putId(incrTableId("user_florist_task"));
    }

    public JsonObject insert() {
        return super.insert(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, LISTKEY, getListKey(), getListItemKey(), ORDER_LIST);
    }

    public void delete() {
        super.delete(getKey(), id, UPDATE_DB_TYPE, userId, LISTKEY, getListKey(), getListItemKey(), ORDER_LIST);
    }

    /**
     * 根据入库类型更新
     */
    public JsonObject update() {
        return super.update(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, userId, getAllObj(), LISTKEY, getListKey(), getListItemKey());
    }

    /**
     * 实时入队列
     */
    public JsonObject updateQueueNow() {
        return super.update(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, 0, getAllObj(), LISTKEY, getListKey(), getListItemKey());
    }

    public JsonObject getAllObj(){
        JsonObject obj = new JsonObject();
        obj.put("id", id); // id
        obj.put("userId", userId); // 玩家id
        obj.put("taskId", taskId); // 任务表的id
        obj.put("curP", curP); // 当前进度
        obj.put("isRecv", isRecv); // 是否领取奖励
        return obj;
    }

    public PBUserFloristTask.Builder takeInitBuilder() {
        PBUserFloristTask.Builder sb = PBUserFloristTask.newBuilder();
        if (taskId != 0) {
            sb.setTaskId(taskId);
        }
        if (curP != 0) {
            sb.setCurP(curP);
        }
        if (isRecv == 1) {
            sb.setIsRecv(true);
        }
        return sb;
    }

    public PBUserFloristTask.Builder takeBuilder() {
        if (updateObj == null || updateObj.isEmpty()) {
            return takeInitBuilder();
        }
        PBUserFloristTask.Builder sb = PBUserFloristTask.newBuilder();
        if (isFieldUpdate("taskId")) {
            sb.setTaskId(taskId);
        }
        if (isFieldUpdate("curP")) {
            sb.setCurP(curP);
        }
        if (isFieldUpdate("isRecv")) {
            sb.setIsRecv(isRecv == 1);
        }
        return sb;
    }

    public long getId() {
        return id;
    }

    public void putId(long id) {
        if (this.id != id) {
            this.id = id;
            updateRedisHashField(getKey(), "id", this.id);
        }
    }

    public void addId(long value) {
        this.id += value;
        updateRedisHashField(getKey(), "id", this.id);
    }

    public long getUserId() {
        return userId;
    }

    public void putUserId(long userId) {
        if (this.userId != userId) {
            this.userId = userId;
            updateRedisHashField(getKey(), "userId", this.userId);
        }
    }

    public void addUserId(long value) {
        this.userId += value;
        updateRedisHashField(getKey(), "userId", this.userId);
    }

    public int getTaskId() {
        return taskId;
    }

    public void putTaskId(int taskId) {
        if (this.taskId != taskId) {
            this.taskId = taskId;
            updateRedisHashField(getKey(), "taskId", this.taskId);
        }
    }

    public void addTaskId(int value) {
        this.taskId += value;
        updateRedisHashField(getKey(), "taskId", this.taskId);
    }

    public long getCurP() {
        return curP;
    }

    public void putCurP(long curP) {
        if (this.curP != curP) {
            this.curP = curP;
            updateRedisHashField(getKey(), "curP", this.curP);
        }
    }

    public void addCurP(long value) {
        this.curP += value;
        updateRedisHashField(getKey(), "curP", this.curP);
    }

    public byte getIsRecv() {
        return isRecv;
    }

    public void putIsRecv(byte isRecv) {
        if (this.isRecv != isRecv) {
            this.isRecv = isRecv;
            updateRedisHashField(getKey(), "isRecv", this.isRecv);
        }
    }

    public void addIsRecv(byte value) {
        this.isRecv += value;
        updateRedisHashField(getKey(), "isRecv", this.isRecv);
    }

}